package com.canbot.u05.activity.agenda.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import com.canbot.u05.R
import com.canbot.u05.activity.agenda.calendar.getTime
import com.canbot.u05.activity.agenda.dao.AgendaDataTable
import com.canbot.u05.activity.agenda.interfaces.IAgendaHomeLisenter

/**
 * @author yinxiaowei
 * @date 2019/5/31
 */
class AgendaHomeAdapter(private var agendaBeanList: ArrayList<AgendaDataTable>, context: Context, private val iAgendaHomeLinsenter: IAgendaHomeLisenter?) : RecyclerView.Adapter<AgendaHomeAdapter.AgendaHomeViewHolder>() {
    private val layoutInflater: LayoutInflater = LayoutInflater.from(context)
    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): AgendaHomeViewHolder {
        val view = layoutInflater.inflate(R.layout.agenda_list_item, viewGroup, false)
        return AgendaHomeViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(agendaHomeViewHolder: AgendaHomeViewHolder, i: Int) {
        val agendaBean = agendaBeanList[i].agendaBean
        agendaHomeViewHolder.tvAgendaName.text = String.format("%d.%s", agendaHomeViewHolder.adapterPosition + 1, agendaBean.agendaType.type)
        if (agendaBean.endTime == null) {
            agendaHomeViewHolder.tvAgendaTime.text = "${getTime(agendaBean.startTime)}} "
        } else {
            agendaHomeViewHolder.tvAgendaTime.text = "${getTime(agendaBean.startTime)} - ${getTime(agendaBean.endTime)} "
        }
        agendaHomeViewHolder.tvAgendaPos.text = agendaBean.pos?.name
        agendaHomeViewHolder.ivAgendaEdit.setOnClickListener {
            iAgendaHomeLinsenter?.onEdit(agendaHomeViewHolder.adapterPosition)
        }
        agendaHomeViewHolder.ivAgendaDelete?.setOnClickListener {
            iAgendaHomeLinsenter?.onDelete(agendaHomeViewHolder.adapterPosition)
        }
    }

    override fun getItemCount(): Int {
        return agendaBeanList.size
    }

    inner class AgendaHomeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvAgendaName = itemView.findViewById<TextView>(R.id.tv_agenda_name)
        var tvAgendaTime = itemView.findViewById<TextView>(R.id.tv_agenda_time)
        var tvAgendaPos = itemView.findViewById<TextView>(R.id.tv_agenda_pos)
        var ivAgendaEdit = itemView.findViewById<Button>(R.id.iv_agenda_edit)
        var ivAgendaDelete = itemView.findViewById<Button>(R.id.iv_agenda_delete)
    }
}
